package com.wf.course.web.controller.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.goods.GoodsImage;
import com.wf.course.model.goods.GoodsInfo;
import com.wf.course.model.goods.GoodsParam;
import com.wf.course.model.goods.GoodsType;
import com.wf.course.service.IGoodsInfoService;
import com.wf.course.service.IGoodsParamService;
import com.wf.course.service.IGoodsTypeService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 列表Controller
 */
@RequestMapping("goodsInfo")
@Controller
public class GoodsInfoController {

	@Autowired
	private IGoodsInfoService goodsInfoService;
	@Autowired
	private IGoodsTypeService goodsTypeService;
	@Autowired
	private IGoodsParamService goodsParamService;

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param typeName
	 * @param model
	 * @return
	 */
	@RequestMapping("goodsInfoList")
	public String goodsInfoList(HttpSession httpSession,
			@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "goodsName", required = false) String goodsName, 
			@RequestParam(value = "goodsTypeCode", required = false) String goodsTypeCode, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("goodsName", goodsName);
		cons.put("goodsTypeCode", goodsTypeCode);
		PageBean<GoodsInfo> pageBean = new PageBean<GoodsInfo>(pageNo, pageSize, null, cons);
		pageBean.setUrl("goodsInfo/goodsInfoList");
		this.goodsInfoService.indexInfo(pageBean);
		
		 List<GoodsType> goodsTypes = goodsTypeService.queryGoodsTypes();
		 model.addAttribute("goodsTypes",goodsTypes);
		
		model.addAttribute("page", pageBean);
		httpSession.setAttribute("page", pageBean);
		
		return "goods/goodsInfoList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model,HttpSession httpSession) {
		 
		if (id != null && id != 0) {
			GoodsInfo goodsInfo = goodsInfoService.queryGoodsInfoById(id);
			model.addAttribute("goodsInfo", goodsInfo);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 
		 List<GoodsType> goodsTypes = goodsTypeService.queryGoodsTypes();
		 model.addAttribute("goodsTypes",goodsTypes);
		 
		 List<GoodsParam> goodsParams = goodsParamService.queryGoodsParamByCondition(id,0);
		 if(goodsParams!=null && goodsParams.size()>0){
			 GoodsParam goodsParam  = goodsParams.get(0);
			 goodsParam.setDetail(goodsParam.getParamValue());
			 goodsParam.setParamValue(null);
			 model.addAttribute("goodsParam",goodsParam);
		 }
		 		
		 
		return "goods/goodsInfoEdit";
	}

	/**
	 * 新增和修改
	 * 
	 * @param adInfo
	 * @param imgFile
	 * @param h5File
	 * @param request
	 * @return
	 */
	@LogInfo("新增/修改商品信息")
	@RequestMapping(value = "goodsInfoEdit" , produces = "text/html;charset=UTF-8")
	public String goodsInfoEdit(@ModelAttribute GoodsInfo goodsInfo,  
			MultipartFile imgFile,
			@ModelAttribute GoodsImage goodsImage,
			@RequestParam("goodsImgFiles") MultipartFile[]  goodsImgFiles,
			@RequestParam String detail,
			@RequestParam("paramValues") String[]  paramValues,
			HttpServletRequest request,HttpSession httpSession,Model model) {
		
			goodsInfoService.saveOrUpdateGoodsInfo(goodsInfo,imgFile,goodsImage,goodsImgFiles,detail,paramValues);
		
			//保存后，跳转到之前的列表页
			@SuppressWarnings("unchecked")
			PageBean<T> page =  (PageBean<T>) httpSession.getAttribute("page");
			PageBean<GoodsInfo> pageBean = new PageBean<GoodsInfo>(page.getActulPageNo(), page.getPageSize(), null, page.getConMap());
			
			pageBean.setUrl("goodsInfo/goodsInfoList");
			this.goodsInfoService.indexInfo(pageBean);
			
			 List<GoodsType> goodsTypes = goodsTypeService.queryGoodsTypes();
			 model.addAttribute("goodsTypes",goodsTypes);
			
			model.addAttribute("page", pageBean);
			 
			return "goods/goodsInfoList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除商品信息")
	@RequestMapping(value = "remove/{id}")
	public String removeGoodsInfo(@PathVariable("id") Integer id) {
		
		goodsInfoService.deleteById(id);
		goodsParamService.deleteByGoodsId(id);
		
		return "redirect:/goodsInfo/goodsInfoList";
	}
	

}
